﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;

namespace Caifu.Datas
{
	/// <summary>
	/// 非交易日期。
	/// </summary>
	public class NontradeDateData
	{
		/// <summary>
		/// 
		/// </summary>
		public static readonly NontradeDateData Instance = new NontradeDateData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 添加非交易日记录。
		/// </summary>
		/// <param name="nontradeDate">非交易日期信息对象</param>
		public void AddNontradeDate(NontradeDate nontradeDate)
		{
			nontradeDate.Detach();
			dbSession.Save<NontradeDate>(nontradeDate);
		}

		/// <summary>
		/// 修改非交易日记录。
		/// 主键Date不修改。
		/// </summary>
		/// <param name="nontradeDate">非交易日期信息对象</param>
		public void UpdateNontradeDate(NontradeDate nontradeDatefund)
		{
			nontradeDatefund.Attach();
			dbSession.Save<NontradeDate>(nontradeDatefund);
		}

		/// <summary>
		/// 删除非交易日记录
		/// </summary>
		/// <param name="date">指定日期</param>
		public void DeleteNontradeDate(DateTime date)
		{
			dbSession.Delete<NontradeDate>(date);
		}

		/// <summary>
		/// 获取非交易日记录。
		/// </summary>
		/// <param name="date">指定日期</param>
		/// <returns></returns>
		public NontradeDate GetNontradeDate(DateTime date)
		{
			return dbSession.Single<NontradeDate>(date);
		}

		/// <summary>
		/// 获取所有非交易日。
		/// </summary>
		/// <returns></returns>
		public IList<NontradeDate> GetAllNontradeDate()
		{
			return dbSession.From<NontradeDate>().ToList();
		}

		/// <summary>
		/// 获取指定年份所有非交易日。
		/// </summary>
		/// <param name="year">指定年份</param>
		/// <returns></returns>
		public IList<NontradeDate> GetNontradeDates(int year)
		{
			var where = NontradeDate._.Date >= new DateTime(year, 1, 1)
				&& NontradeDate._.Date <= new DateTime(year, 12, 31);

			return dbSession.From<NontradeDate>().Where(where).ToList();
		}
	}
}

